package com.justgame.project.service.just.controller;

import com.justgame.project.base.annotation.ProtectAccess;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.service.api.dto.JustGameCloudDTO;
import com.justgame.project.cloud.common.entity.PageRequest;
import com.justgame.project.service.just.service.JustGameCloudService;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/just/game-cloud")
@AllArgsConstructor
public class JustGameCloudController {

    private final JustGameCloudService gameCloudService;

    @GetMapping("/list")
    public Result<?> getList(PageRequest pageRequest, JustGameCloudDTO dto) {
        return Result.OK(gameCloudService.getList(pageRequest, dto));
    }

    @PostMapping("/save")
    @RequiresRoles("admin")
    @ProtectAccess
    public Result<?> addGameType(@Valid @RequestBody JustGameCloudDTO dto) {
        return Result.OK(gameCloudService.save(dto));
    }

    @DeleteMapping("/delete")
    @RequiresRoles("admin")
    @ProtectAccess
    public Result<?> deleteGameType(@RequestBody JustGameCloudDTO dto) {
        return Result.OK(gameCloudService.Delete(dto));
    }

    @PostMapping("/updateById")
    @RequiresRoles("admin")
    @ProtectAccess
    public Result<?> updateById(@RequestBody JustGameCloudDTO dto) {
        return Result.OK(gameCloudService.updateById(dto));
    }

    /**
     * 通过游戏id获取相关网盘资源信息（会校验是否已购买并且未过期）
     *
     * @param gameId
     * @return
     */
    @GetMapping("/obtain-by-gid")
    @ProtectAccess
    public Result<?> obtainResource(@NotBlank(message = "id不能为空！") @RequestParam String gameId) {
        return Result.OK(gameCloudService.obtainResourceByGid(gameId));
    }

}
